﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MockDataAccess;
using MockDataTransferObject;
using MockCommon;
using System.Collections.ObjectModel;

namespace MockBusiness
{
    public class ReasonBusiness
    {
        #region Initialize
        private static ReasonBusiness _instance;
        private readonly ReasonDataAccess _reasonDataAccess;

        /// <summary>
        /// Gets the reason data test.
        /// </summary>
        public Collection<Reason> ReasonDataTest
        {
            get
            {
                return _reasonDataAccess.AffectedRows;
            }
        }

        /// <summary>
        /// Prevents a default instance of the <see cref="ReasonBusiness"/> class from being created.
        /// </summary>
        private ReasonBusiness()
        {
            _reasonDataAccess = new ReasonDataAccess();
        }

        /// <summary>
        /// Prevents a default instance of the <see cref="ReasonBusiness"/> class from being created.
        /// </summary>
        /// <param name="isFake">if set to <c>true</c> [is fake].</param>
        private ReasonBusiness(bool isFake)
        {
            _reasonDataAccess = new ReasonDataAccess(isFake);
        }

        /// <summary>
        /// Retrieves the instance.
        /// </summary>
        /// <returns></returns>
        public static ReasonBusiness RetrieveInstance()
        {
            if (_instance == null)
                _instance = new ReasonBusiness();
            return _instance;
        }

        /// <summary>
        /// Retrieves the instance.
        /// </summary>
        /// <param name="isFake">if set to <c>true</c> [is fake].</param>
        /// <returns></returns>
        public static ReasonBusiness RetrieveInstance(bool isFake)
        {
            if (_instance == null)
                _instance = new ReasonBusiness(isFake);
            return _instance;
        }
        #endregion

        #region CUD
        /// <summary>
        /// Creates the specified reason.
        /// </summary>
        /// <param name="reason">The reason.</param>
        public void Create(Reason reason)
        {
            if (reason == null)
            {
                throw new MockException("Reason is null", "Business");
            }
            if (_reasonDataAccess.Exist(reason.ReasonId))
            {
                throw new MockException("Reason id was existed", "Business");
            }
            _reasonDataAccess.Create(reason);
        }
        #endregion

        #region R
        /// <summary>
        /// Gets the name of the reason by status.
        /// </summary>
        /// <param name="timesheetId">The timesheet id.</param>
        /// <param name="statusName">Name of the status.</param>
        /// <returns></returns>
        public object GetReasonByStatusName(Guid timesheetId, string statusName)
        {
            if (statusName != null)
                return _reasonDataAccess.GetReasonTextByStatusName(timesheetId, statusName);
            return null;
        }
        #endregion
    }
}
